// 更新博文
$("#updateArticleBtn").on("click", function () {
    var articleId = $(".articleId").val();
    var articlePicture = $(".articlePicture").val();
    var articleTitle = $(".articleTitle").val();
    var articleContent = editor.txt.text();
    if (articlePicture != null && articlePicture.length != 0) {
        if (articleTitle != null && articleTitle.length != 0) {
            if (articleContent != null && articleContent) {
                $.ajax({
                    type: "POST",
                    url: "/articleManage/updateArticle",
                    data: {articleId: articleId, articleTitle: articleTitle, articlePicture: articlePicture, articleContent: editor.txt.html()},
                    dataType: "json",
                    success: function (data) {
                        if (data.result == "success") {
                            $(".article-message").html("更新成功").css({"color": "green"});
                        }
                    },
                    error: function () {
                        $(".article-message").html("ajax 服务器错误").css({"color": "red"});
                    }
                });
            } else {
                $(".article-message").html("请输入博文内容").css({"color": "red"});
            }
        } else {
            $(".article-message").html("请输入博文标题").css({"color": "red"});
        }
    } else {
        $(".article-message").html("请上传配图").css({"color": "red"});
    }
});

// 回车搜索博文
function onKeyDownArticle(event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 13) {
        var content = $.trim($(".searchContentArticle").val());
        if (content == null || content.length != 0) {
            window.location.href = "/articleManage/searchArticle?content=" + content;
        } else {
            alert("搜索条件不能为空呦");
        }
    }
}

// 按钮搜索博文
$(".searchBtnArticle").on("click", function () {
    var content = $.trim($(".searchContentArticle").val());
    if (content == null || content.length != 0) {
        window.location.href = "/articleManage/searchArticle?content=" + content;
    } else {
        alert("搜索条件不能为空呦");
    }
});

// 删除博文
function deleteArticle(articleId) {
    $("#deleteArticleModal").modal({
        keyboard: false
    });
    $("#deleteArticleBtn").on("click", function () {
        if (articleId != null && articleId.length != null) {
            $.ajax({
                type: "POST",
                url: "/articleManage/deleteArticle",
                data: {articleId: articleId},
                dataType: "json",
                success: function (data) {
                    if (data.result == "success") {
                        window.location.reload();
                    } else {
                        alert("BUG：删除博文失败");
                    }
                },
                error: function () {
                    $(".article-message").html("ajax 服务器错误").css({"color": "red"});
                }
            });
        }
    });
}

// 上传博文配图-更新
$(".updateArticlePictureInput").bind("change", function () {
    $(".articlePictureImg img").remove();
    $(".articlePicture").val(" ");
    console.log("清除缓存图片");
    var data = uploadfile("updateArticlePictureInput");
    if (data.code == 100) {
        $(".article-photo-message").html(data.msg).css({"color": "red"});
    } else {
        console.log(data.path);
        $(".articlePictureImg").attr({"src": data.path});
        $(".articlePicture").val(data.path);
    }
});

// 上传博文配图-新增
$(".articlePictureInput").bind("change", function () {
    $(".articlePictureImg img").remove();
    $(".articlePicture").val(" ");
    console.log("清除缓存图片");
    var data = uploadfile("articlePictureInput");
    if (data.code == 100) {
        $(".article-photo-message").html(data.msg).css({"color": "red"});
    } else {
        console.log(data.path);
        $(".articlePictureImg").append('<img src="' + data.path + '" style="width: 300px; height: 180px;" class="img-rounded img-responsive" alt="404">');
        $(".articlePicture").val(data.path);
    }
});

// 发表博文
$("#addArticleBtn").on("click", function () {
    var articlePicture = $(".articlePicture").val();
    var articleTitle = $(".articleTitle").val();
    var articleContent = editor.txt.text();
    if (articlePicture != null && articlePicture.length != 0) {
        if (articleTitle != null && articleTitle.length != 0) {
            if (articleContent != null && articleContent) {
                $.ajax({
                    type: "POST",
                    url: "/articleManage/addArticle",
                    data: {articleTitle: articleTitle, articlePicture: articlePicture, articleContent: editor.txt.html()},
                    dataType: "json",
                    success: function (data) {
                        if (data.result == "success") {
                            $(".article-message").html("发表成功").css({"color": "green"});
                        }
                    },
                    error: function () {
                        $(".article-message").html("ajax 服务器错误").css({"color": "red"});
                    }
                });
            } else {
                $(".article-message").html("请输入博文内容").css({"color": "red"});
            }
        } else {
            $(".article-message").html("请输入博文标题").css({"color": "red"});
        }
    } else {
        $(".article-message").html("请上传配图").css({"color": "red"});
    }
});